package com.golang.api.repository;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.golang.api.domain.*;
import com.golang.api.dto.request.ActivitySearchDto;
import com.golang.api.mapper.*;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Repository
public class ActivityRepository {

    @Resource
    ActivityMapper activityMapper;

    @Resource
    ActivityProcessMapper activityProcessMapper;

    @Resource
    ActivityTagMapper activityTagMapper;

    @Resource
    ActivityTicketMapper activityTicketMapper;

    @Resource
    ActivityPicMapper activityPicMapper;


    public IPage<Activity> queryActivityInfoPage(ActivitySearchDto query) {
        LambdaQueryWrapper<Activity> queryWrapper = new LambdaQueryWrapper<>();
        //分类
        queryWrapper.eq(Objects.nonNull(query.getActivityClassification()), Activity::getActivityClassification, query.getActivityClassification());
        //开始时间
        queryWrapper.ge(Objects.nonNull(query.getStartTime()), Activity::getStartTime, query.getStartTime());
        //结束时间
        queryWrapper.le(Objects.nonNull(query.getEndTime()), Activity::getStartTime, query.getEndTime());
        //cityCode
        //queryWrapper.eq(Objects.nonNull(query.getCityCode()), Activity::getCityCode, query.getCityCode());
        //活动形式
        queryWrapper.eq(Objects.nonNull(query.getActivityType()), Activity::getActivityType, query.getActivityType());
        //活动标题
        queryWrapper.like(StrUtil.isNotEmpty(query.getActivityTitle()), Activity::getActivityTitle, query.getActivityTitle());
        //活动详情
        queryWrapper.like(StrUtil.isNotEmpty(query.getActivityIntroduction()), Activity::getActivityIntroduction, query.getActivityIntroduction());
        //标签Id
        if (Objects.nonNull(query.getTagId())) {
            LambdaQueryWrapper<ActivityTag> tagQueryWrapper = new LambdaQueryWrapper<>();
            tagQueryWrapper.eq(ActivityTag::getTagId, query.getActivityClassification());

            List<ActivityTag> activityTags = activityTagMapper.selectList(tagQueryWrapper);
            List<String> activityIds = activityTags.stream().map(ActivityTag::getActivityId).collect(Collectors.toList()).stream().distinct().collect(Collectors.toList());

            if (CollUtil.isNotEmpty(activityIds)) {
                queryWrapper.in(Activity::getActivityId, activityIds);
            }
        }
        Page<Activity> page = new Page<>(query.getCurrent(), query.getSize());
        return activityMapper.selectPage(page, queryWrapper);
    }


    public int insertActivityBasic(Activity entity) {

        return activityMapper.insert(entity);


    }

    public int updateActivityBasic(Activity entity) {
        LambdaUpdateWrapper<Activity> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Activity::getActivityId, entity.getActivityId());
        return activityMapper.update(entity, updateWrapper);
    }


    public int updateActivitySetting(Activity entity) {
        LambdaUpdateWrapper<Activity> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Activity::getActivityId, entity.getActivityId());
        return activityMapper.update(entity,updateWrapper);
    }

    public int insertActivityProcess(ActivityProcess entity) {
        return activityProcessMapper.insert(entity);
    }


    public int deleteProcessByActivityId(String activityId) {

        LambdaQueryWrapper<ActivityProcess> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Objects.nonNull(activityId), ActivityProcess::getActivityId, activityId);
        return activityProcessMapper.delete(queryWrapper);
    }


    public int insertActivityTag(ActivityTag entity) {
        return activityTagMapper.insert(entity);
    }


    public int deleteTagByActivityId(String activityId) {

        LambdaQueryWrapper<ActivityTag> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Objects.nonNull(activityId), ActivityTag::getActivityId, activityId);
        return activityTagMapper.delete(queryWrapper);
    }


    public int insertActivityTicket(ActivityTicket entity) {
        return activityTicketMapper.insert(entity);
    }


    public int deleteTicketByActivityId(String activityId) {

        LambdaQueryWrapper<ActivityTicket> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Objects.nonNull(activityId), ActivityTicket::getActivityId, activityId);
        return activityTicketMapper.delete(queryWrapper);
    }


    public Activity queryActivity(String activityId) {

        LambdaQueryWrapper<Activity> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(Activity::getActivityId, activityId);

        return activityMapper.selectOne(queryWrapper);
    }


    public List<ActivityProcess> queryActivityProcess(String activityId) {

        LambdaQueryWrapper<ActivityProcess> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(ActivityProcess::getActivityId, activityId);


        return activityProcessMapper.selectList(queryWrapper);
    }

    public List<ActivityTag> queryActivityTag(String activityId) {

        LambdaQueryWrapper<ActivityTag> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(ActivityTag::getActivityId, activityId);


        return activityTagMapper.selectList(queryWrapper);
    }


    public ActivityTicket queryActivityTicket(Long id) {
        return activityTicketMapper.selectById(id);
    }


    public List<ActivityTicket> queryActivityTicket(String activityId) {

        LambdaQueryWrapper<ActivityTicket> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(ActivityTicket::getActivityId, activityId);

        return activityTicketMapper.selectList(queryWrapper);
    }


    public int insertActivityPic(ActivityPic entity) {
        return activityPicMapper.insert(entity);
    }

    public int deletePicByActivityId(String activityId) {

        LambdaQueryWrapper<ActivityPic> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Objects.nonNull(activityId), ActivityPic::getActivityId, activityId);
        return activityPicMapper.delete(queryWrapper);
    }

}
